 

cd "$datadir"
clear all

* First, Fig A9 
* commenting out a data prep step that includes confidential data from 
* voter registration files. We still provide the product of the data prep.

/*
use "CEDA_matched_to_vrd.dta", replace


gen address= addressnumber+" "+ housefractionnumber+ " "+ addressnumbersuffix+ " "+ streetdirprefix+" "+  streetname+" "+  streettype+" "+  streetdirsuffix+ " "+ unittype+" "+  unitnumber
replace address=subinstr(address,"  "," ",.)
replace address=subinstr(address,"  "," ",.)
replace address=subinstr(address,"  "," ",.)
replace address=subinstr(address,"  "," ",.)
replace address=subinstr(address,"  "," ",.)
replace address=trim(address)

rename place city

merge m:1 address city   zcta5 using cedavrdaddressesonly_geocodio_for_merge
 keep if _merge==3
 drop _merge
 
 
 * merge in distrct dates
merge m:1 placefips using CA_all_districted_cities_2008_2020_w_placefips_for_merge_sept2024, gen(_merge_switch)
drop if   _merge_switch==2

  
 
drop fipspl

 gen fipspl= placefips-600000

 merge m:1 fipspl using  "is contained in CEDA_ethnicity_vote_fips_08_15_17_v13.dta"
keep if _merge==3
drop _merge
 
 merge m:1 placefips using "more placefips demos.dta", gen(_merge_more)
drop if _merge_more==2

drop if placefips==.
 
	 
 collapse (mean) matched, by(placefips fipspl c_totalpop c_cvap_total c_share_pop_over18 c_share_pop_over65 c_share_renter_occ_housing c_share_ratio_inc_pov_7 c_share_cvap_aapi c_share_cvap_black c_share_cvap_white c_share_cvap_hisp c_share_cvap_other switch always_D_new countyfips)
 
 save "collapsed data for fig a9.dta", replace
 */
 
  use "collapsed data for fig a9.dta", replace

 
 drop if placefips==.
 
 drop if always_D==1
 
 reg matched switch   c_totalpop   c_share_pop_over18 c_share_pop_over65 c_share_renter_occ_housing c_share_ratio_inc_pov_7 c_share_cvap_aapi c_share_cvap_black   c_share_cvap_hisp c_share_cvap_other 
 
 foreach x in c_totalpop   c_share_pop_over18 c_share_pop_over65 c_share_renter_occ_housing c_share_ratio_inc_pov_7 c_share_cvap_aapi c_share_cvap_black   c_share_cvap_hisp c_share_cvap_other  {
sum `x', d
gen z_`x'=(`x'-r(mean))/r(sd)
}

 reg matched switch z_*
 
 label var switch "Switches to district during sample (0/1)"
 
 label variable z_c_totalpop "Total pop. (std. norm.)"
label variable z_c_share_pop_over18 "Share over 18 (std. norm.)"
label variable z_c_share_pop_over65 "Share over 65 (std. norm.)"
label variable z_c_share_renter_occ_housing "Share renter-occ. housing (std. norm.)"
label variable z_c_share_ratio_inc_pov_7 "Share income over 2x pov. (std. norm.)"
label variable z_c_share_cvap_aapi "Share CVAP AAPI (std. norm.)"
label variable z_c_share_cvap_black "Share CVAP Black (std. norm.)"
label variable z_c_share_cvap_hisp "Share CVAP Hisp. (std. norm.)"
label variable z_c_share_cvap_other "Share CVAP Other Race/Eth. (std. norm.)"

 
 
coefplot, drop(_cons)
 graph export "$dist_output/appx_matched_chars.png", as(png) name("Graph") replace 
 

 
 
 
 * Produce data to populate Figure a8. (figure is actually produced in GIS)

 
 drop if always_D==1
 collapse (mean) matched, by(countyfips)

egen float matched_cut3 = cut(matched), group(3)
gen matched_cut3_str="Low" if matched_cut3==0
replace matched_cut3_str="Mid." if matched_cut3==1
replace matched_cut3_str="High" if matched_cut3==2
 
 save "$dist_output/matchedness by county for mapping.dta", replace
